﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Data;
using System.Reflection;

namespace QLChoDiaOc.Process
{
    class OtherHandle
    {
        SelectInfo _selectInfo = new SelectInfo();
        ListInfo _listInfo = new ListInfo();

        public void IsNumber(TextBox m_txtTextBox)
        {
            for (int i = 0; i < m_txtTextBox.Text.Trim().Length; i++)
            {
                if (m_txtTextBox.Text[i] != '0' &&
                    m_txtTextBox.Text[i] != '1' &&
                    m_txtTextBox.Text[i] != '2' &&
                    m_txtTextBox.Text[i] != '3' &&
                    m_txtTextBox.Text[i] != '4' &&
                    m_txtTextBox.Text[i] != '5' &&
                    m_txtTextBox.Text[i] != '6' &&
                    m_txtTextBox.Text[i] != '7' &&
                    m_txtTextBox.Text[i] != '8' &&
                    m_txtTextBox.Text[i] != '9' &&
                    m_txtTextBox.Text[i] != '.')
                {
                    //return true;
                    MessageBox.Show("Chỉ nhập số", "Lỗi");
                    m_txtTextBox.Text = "";
                    m_txtTextBox.Focus();
                    //return false;
                }
                //else
                //{
                //MessageBox.Show("Chỉ nhập số", "Lỗi");
                //m_txtTextBox.Text = "";
                //m_txtTextBox.Focus();
                //return false;
                // return true;
                //}
            }
            //return true;
        }

        public void TinhTienQCToBuom(int SoLuong, ref TextBox DonGia, ref TextBox ThanhTien)
        {
            if (SoLuong < 100)
                DonGia.Text = "5000";
            if (SoLuong >= 100 && SoLuong < 200)
                DonGia.Text = "4000";
            if (SoLuong >= 200)
                DonGia.Text = "3000";
            int temp = int.Parse(DonGia.Text) * SoLuong;
            if (temp <= 0)
                MessageBox.Show("Kiểm Tra Lại Số Lượng", "Thông Báo");
            else
                ThanhTien.Text = temp.ToString();
        }

        public void TinhTienQCBang(string TenLoaiBang, ref TextBox DonGia, ref TextBox ThanhTien)
        {
            DonGia.Text = _selectInfo.SelectDonGiaLoaiBang(TenLoaiBang).ToString();
            ThanhTien.Text = (int.Parse(DonGia.Text) * 1.1).ToString();
        }

        public void TinhTienQCBao(string TenBao, TextBox SoKy, ref TextBox ThanhTien)
        {
            int _iDonGia = _selectInfo.SelectDonGiaToBao(TenBao);
            if (SoKy.Text == "")
                SoKy.Text = "0";
            int temp = _iDonGia * int.Parse(SoKy.Text);
            if (temp <= 0)
                MessageBox.Show("Kiểm Tra Lại Thông Số", "Thông Báo");
            else
                ThanhTien.Text = temp.ToString();
        }

        public void TinhTongTien(TextBox TienQCBao, TextBox TienQCToBuom, TextBox TienQCBang, ref TextBox TongTien)
        {
            if (TienQCBao.Text == "")
                TienQCBao.Text = "0";
            if (TienQCToBuom.Text == "")
                TienQCToBuom.Text = "0";
            if (TienQCBang.Text == "")
                TienQCBang.Text = "0";
            TongTien.Text = (int.Parse(TienQCBao.Text) + int.Parse(TienQCToBuom.Text) + int.Parse(TienQCBang.Text)).ToString();
        }

        public void TiLeQuangCao(string Ktra, ref TextBox MotQC, ref TextBox TongQC, ref TextBox TiLe)
        {
            switch (Ktra)
            {
                case "Bang":
                    MotQC.Text = _listInfo.ListKhachHangQCBang().Count.ToString();
                    TongQC.Text = (_listInfo.ListKhachHangQCBang().Count + _listInfo.ListKhachHangQCBao().Count + _listInfo.ListKhachHangQCToBuom().Count).ToString();
                    TiLe.Text = (float.Parse(MotQC.Text) * 100 / float.Parse(TongQC.Text)).ToString();
                    break;
                case "Bao":
                    MotQC.Text = _listInfo.ListKhachHangQCBao().Count.ToString();
                    TongQC.Text = (_listInfo.ListKhachHangQCBang().Count + _listInfo.ListKhachHangQCBao().Count + _listInfo.ListKhachHangQCToBuom().Count).ToString();
                    TiLe.Text = (float.Parse(MotQC.Text) * 100 / float.Parse(TongQC.Text)).ToString();
                    break;
                case "ToBuom":
                    MotQC.Text = _listInfo.ListKhachHangQCToBuom().Count.ToString();
                    TongQC.Text = (_listInfo.ListKhachHangQCBang().Count + _listInfo.ListKhachHangQCBao().Count + _listInfo.ListKhachHangQCToBuom().Count).ToString();
                    TiLe.Text = (float.Parse(MotQC.Text) * 100 / float.Parse(TongQC.Text)).ToString();
                    break;
                default:
                    break;
            }
        }

        public static DataTable DataViewAsDataTable(DataView dv)
        {
            DataTable dt = dv.Table.Clone();
            foreach (DataRowView drv in dv)
                dt.ImportRow(drv.Row);
            return dt;
        }


        public DataTable ConvertDataGridToDataTable(DataGrid _dataGrid)
        {
            //DataTable dt = new DataTable();
            //DataRow dr;
            //dt.Columns.

            // DataView dv = (DataView)_dataGrid.ItemsSource;
            // dt = dv.Table.DataSet.Tables[0];
            //_dataGrid.DataSource 

            //foreach (DataGrid item in _dataGrid.Items)
            //{
            //    dr = dt.NewRow();
            //    dr[
            //}

            //DataView view = (DataView) _dataGrid.ItemsSource;
            //DataTable table = DataViewAsDataTable(view);
            DataTable dt = ((DataView)_dataGrid.ItemsSource).ToTable();

            return dt;
        }

        public DataSet ConvertDataGridToDataSet(DataGrid _dataGrid)
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            dt = ConvertDataGridToDataTable(_dataGrid);

            ds.Tables.Add(dt);
            return ds;
        }

        public DataSet ConvertDataTableToDataSet(DataTable _dataTable)
        {
            DataSet ds = new DataSet();

            ds.Tables.Add(_dataTable);
            return ds;
        }

        public DataTable ToDataTable<T>(List<T> items)
        {
            var tb = new DataTable(typeof(T).Name);

            PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (PropertyInfo prop in props)
            {
                Type t = GetCoreType(prop.PropertyType);
                tb.Columns.Add(prop.Name, t);
            }

            foreach (T item in items)
            {
                var values = new object[props.Length];

                for (int i = 0; i < props.Length; i++)
                {
                    values[i] = props[i].GetValue(item, null);
                }

                tb.Rows.Add(values);
            }

            return tb;
        }

        public static bool IsNullable(Type t)
        {
            return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
        }

        /// <summary>
        /// Return underlying type if type is Nullable otherwise return the type
        /// </summary>
        public static Type GetCoreType(Type t)
        {
            if (t != null && IsNullable(t))
            {
                if (!t.IsValueType)
                {
                    return t;
                }
                else
                {
                    return Nullable.GetUnderlyingType(t);
                }
            }
            else
            {
                return t;
            }
        }
    }
}
